package com.shengwei.truck.driver.vm;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.shengwei.truck.driver.bean.JsonPostFileData;
import com.shengwei.truck.driver.bean.JsonPoundsState;
import com.shengwei.truck.driver.bean.PoundsListBean;
import com.shengwei.truck.driver.bean.Result;
import com.shengwei.truck.driver.model.DataSource;
import com.shengwei.truck.driver.model.Repository;

import java.io.File;

/**
 * 个人信息ViewModel层
 */
public class PostPoundsViewModel extends ViewModel {
    String TAG  = "PostPoundsViewModel";
    private Repository mRepository; //M层引用
    MutableLiveData<Result<String>> postPoundsLiveData = new MutableLiveData<>();
    MutableLiveData<Result<JsonPostFileData>> outPoundsPicLiveData = new MutableLiveData<>();
    MutableLiveData<Result<JsonPostFileData>> recvPoundsPicLiveData = new MutableLiveData<>();
    MutableLiveData<Result<JsonPoundsState>> checkPoundsStateLiveData = new MutableLiveData<>(); //检查磅单状态


    /**
     * 构造函数
     * @param repository
     */
    public PostPoundsViewModel(Repository repository) {
        mRepository = repository;
    }

    public PostPoundsViewModel() {
        mRepository = Repository.getInstance(DataSource.getSingleInstance());
    }

    public MutableLiveData<Result<JsonPostFileData>> get() {
        return outPoundsPicLiveData;
    }

    public MutableLiveData<Result<JsonPoundsState>> getCheckPoundsStateLiveData() {
        return checkPoundsStateLiveData;
    }

    public MutableLiveData<Result<JsonPostFileData>> getOutPoundsLiveData() {
        return outPoundsPicLiveData;
    }
    public MutableLiveData<Result<JsonPostFileData>> getRecvPoundsLiveData() {
        return recvPoundsPicLiveData;
    }
    public MutableLiveData<Result<String>> getPoundsLiveData() {
        return postPoundsLiveData;
    }

    /**
     * step1.调用文件服务器的上传文件接口，上传图片文件到服务器，服务器生成并返回图片URL地址
     * @param file : 上传的图片文件
     * @param isThumb： 要求服务器是否生成缩率图
     * @param postPoundsType： 上传的榜单图片类型，0出厂榜单图片，1收货榜单图片
     */
    public void postPoundsFile(File file,boolean isThumb,int postPoundsType) {
        if(postPoundsType == 0) {  //出厂榜单
            mRepository.postPicture(file,isThumb,outPoundsPicLiveData);
        }
        else{  //收货榜单
            mRepository.postPicture(file,isThumb,recvPoundsPicLiveData);
        }
    }


    /**
     * step2.调用业务服务器的上传榜单接口，上传图片URL与本次业务的标识如“榜单”，最终完成上传榜单信息等操作
     *
     * @param poundsListInfo:榜单信息
     *
     */
   public void postPoundsListInfo(PoundsListBean poundsListInfo) {
       Log.e(TAG,"postFile will postPounds");
       mRepository.postPoundsListInfo(poundsListInfo,postPoundsLiveData);
   }


    /**
     * 检查磅单状态
     * @param taskId
     */
    public void checkPoundsState(String taskId) {
        mRepository.checkPoundsState(taskId,checkPoundsStateLiveData);
    }

}